/**
 * Copyright (c) 2007,中企动力华南研发中心<br>
 * All rights reserved.
 */
package com.ce.pms.charge.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.charge.dao.ICharStoChoMeterDao;
import com.ce.pms.charge.dao.IChargeItemDao;
import com.ce.pms.charge.dao.IChargeStaDao;
import com.ce.pms.charge.dao.IPeriodLogDao;
import com.ce.pms.charge.model.CharStaChoDomain;
import com.ce.pms.charge.model.ChargeItemDomain;
import com.ce.pms.charge.model.ChargeStaDomain;
import com.ce.pms.charge.service.ICharStoChoMeterService;
import com.ce.pms.charge.vo.CharStaChoMeterVO;
import com.ce.pms.charge.vo.ChargeStaChoVO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.house.dao.IMeterDao;
import com.ce.pms.house.model.MeterType;
import com.ce.pms.house.service.IHouseService;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述：房间标准选用走表服务类<br>
 * @author 罗军林 创建日期：2008-8-7
 */
public class CharStoChoMeterServiceImpl extends PmsBaseServiceImpl implements
		ICharStoChoMeterService {
	ICharStoChoMeterDao charStoChoMeterDao;
	IChargeItemDao chargeItemDao;
	IChargeStaDao chargeStaDao;
	IHouseService houseService;
	IMeterDao meterDao;
	IPeriodLogDao periodLogDao;

	/*
	 * (non-Javadoc)
	 * @see com.ce.pms.charge.service.ICharStoChoMeterService#queryCharStoChoMeter
	 * (com.ce.pms.charge.vo.ChargeStaChoConditionVO)
	 */
	public Page queryCharStoChoMeter(ChargeStaChoVO condition) {
		Page page=charStoChoMeterDao.queryCharStoCho(condition);

		page.setResult( convertDoToVoForList( page.getResult() ) );

		return page;
	}

	/*
	 * (non-Javadoc)
	 * @see com.ce.pms.charge.service.ICharStoChoMeterService#addCharStaChoMeter
	 * (com.ce.pms.charge.vo.CharStaChoMeterVO)
	 */
	public void addCharStaChoMeter(CharStaChoMeterVO charStaChoMeterVO){
		checkParameterIsNull(charStaChoMeterVO.getMeterTypeId(),"charge:meterTypeid_not_null");
		checkParameterIsNull(charStaChoMeterVO.getChargeStaID(), "charge:staid_not_null");
		checkParameterIsNull(charStaChoMeterVO.getChargeItemID(), "charge:itemid_not_null");
		checkCreatorInfo(charStaChoMeterVO.getCreater());
//		checkHouseIdsFormat(charStaChoMeterVO);

		charStoChoMeterDao.addCharStaChoMeter(charStaChoMeterVO);
	}


	/*
	 * (non-Javadoc)
	 * @see com.ce.pms.charge.service.ICharStoChoMeterService#deleteCharStaChoMeter
	 * (java.lang.Long)
	 */
	public void deleteCharStaChoMeter(Long keyword) {
		CharStaChoDomain charStaChoDomain=charStoChoMeterDao.findById(keyword);

		checkPeriodHasArcaculate(charStaChoDomain);

		charStoChoMeterDao.deleteObjectById(keyword);
	}

	/*
	 * (non-Javadoc)
	 * @see com.ce.pms.charge.service.ICharStoChoMeterService#editCharStaChoMeter
	 * (com.ce.pms.charge.vo.CharStaChoMeterVO)
	 */
	public CharStaChoMeterVO editCharStaChoMeter(CharStaChoMeterVO charStaChoMeterVO) {
		checkParameterIsNull(charStaChoMeterVO.getChargeStaID(), "charge:staid_not_null");
		checkParameterIsNull(charStaChoMeterVO.getChargeItemID(), "charge:itemid_not_null");

		ChargeItemDomain chargeItemDomain=chargeItemDao.findById(charStaChoMeterVO.getChargeItemID());
		ChargeStaDomain chargeStaDomain=chargeStaDao.findById(charStaChoMeterVO.getChargeStaID());

		CharStaChoDomain charStaChoDomain=charStoChoMeterDao.findById(charStaChoMeterVO.getKeyword());

		checkPeriodHasArcaculate(charStaChoDomain);

		charStaChoDomain.setChargeItem(chargeItemDomain);
		charStaChoDomain.setChargeSta(chargeStaDomain);
		this.modifyInfo(charStaChoDomain.getSysInfo(), charStaChoMeterVO.getCreater());

		return convertDoToVO(charStaChoDomain);
	}

	/*
	 * (non-Javadoc)
	 * @see com.ce.pms.charge.service.ICharStoChoMeterService#checkExistCharStoChoMeter(com.ce.pms.charge.vo.CharStaChoMeterVO, com.ce.pms.charge.vo.CharStaChoMeterVO)
	 */
	public Boolean checkExistCharStoChoMeter(CharStaChoMeterVO searchCondition,CharStaChoMeterVO oldCondition) {
		return charStoChoMeterDao.checkExistCharStoChoMeter(searchCondition,oldCondition);
	}



	/* (non-Javadoc)
	 * @see com.ce.pms.charge.service.ICharStoChoMeterService#findCharStoChoById(java.lang.Long)
	 */
	public CharStaChoMeterVO findCharStoChoById(Long keyword) {
		this.checkParameterIsNull(keyword, "charge:staChoid_not_null");
		return convertDoToVO( charStoChoMeterDao.findById(keyword) );
	}

	/** 功能描述:DO列表转VO列表<br>
	 * @param list<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-12<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private List<CharStaChoMeterVO> convertDoToVoForList(List<CharStaChoDomain> list) {
		List<CharStaChoMeterVO> result=new ArrayList<CharStaChoMeterVO>();
		for(CharStaChoDomain charsta : list){
			CharStaChoMeterVO charStaChoMeterVO = convertDoToVO(charsta);
			result.add(charStaChoMeterVO);
		}
		return result;
	}

	/**
	 * 功能描述:检查是否做过应收计算<br>
	 * @param charStaChoDomain<br>
	 * Create author:罗军林<br>
	 * Create on:2008-10-31<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private void checkPeriodHasArcaculate(CharStaChoDomain charStaChoDomain) {
		if( periodLogDao.checkPeriodHasArcaculate(charStaChoDomain.getHouse().getBuilding().getKeyword()) ){
			throw new BusinessException("charge:peiord_has_calculate",BusinessException.ERROR);
		}
	}

	/** 功能描述:DO转VO（走表）<br>
	 * @param charsta
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-12<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private CharStaChoMeterVO convertDoToVO(CharStaChoDomain charsta) {
		CharStaChoMeterVO charStaChoMeterVO = new CharStaChoMeterVO();
		charStaChoMeterVO.setKeyword(charsta.getKeyword());
		if( charsta.getHouse() != null){
			charStaChoMeterVO.setHouseId(charsta.getHouse().getKeyword());
			charStaChoMeterVO.setHouseNumber(charsta.getHouse().getHouseNumber());
			charStaChoMeterVO.setOwnerName(houseService.getOwnerNameBy(charsta.getHouse().getHouseNumber()));
		}
		if( charsta.getChargeItem() != null){
			charStaChoMeterVO.setChargeItemID(charsta.getChargeItem().getKeyword());
			charStaChoMeterVO.setChargeItemName(charsta.getChargeItem().getItemName());
		}
		if(charsta.getMeter() != null){
			charStaChoMeterVO.setMeterId( charsta.getMeter().getKeyword() );
			charStaChoMeterVO.setMeterName( charsta.getMeter().getName() );
		}

		if(charsta.getChargeItem().getMeterType() != null){
			MeterType meterType=meterDao.getMeterTypeByCode(charsta.getChargeItem().getMeterType());
			charStaChoMeterVO.setMeterTypeName( meterType.getName() );
			charStaChoMeterVO.setMeterTypeCode( meterType.getCode() );
			charStaChoMeterVO.setMeterTypeId( meterType.getKeyword() );
		}
		if(charsta.getChargeSta() != null){
			charStaChoMeterVO.setChargeStaID(charsta.getChargeSta().getKeyword());
			charStaChoMeterVO.setChargeStaName(charsta.getChargeSta().getStaName());
		}
		return charStaChoMeterVO;
	}

	/**
	 * 功能描述:检查房号列表是否符合指定格式，例1,2,3,4<br>
	 * @param charStaChoMeterVO<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-13<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private void checkHouseIdsFormat(CharStaChoMeterVO charStaChoMeterVO) {
		if( charStaChoMeterVO.getHouseIds() == null ||
				!charStaChoMeterVO.getHouseIds().matches("((\\d,)|(\\d))+?")){
			throw new BusinessException("charge:houseid_format_exception",BusinessException.WARNING);
		}
	}

	/**********************************************************************************/
	public void setChargeItemDao(IChargeItemDao chargeItemDao) {
		this.chargeItemDao = chargeItemDao;
	}

	public void setChargeStaDao(IChargeStaDao chargeStaDao) {
		this.chargeStaDao = chargeStaDao;
	}

	public void setCharStoChoMeterDao(ICharStoChoMeterDao charStoChoMeterDao) {
		this.charStoChoMeterDao = charStoChoMeterDao;
	}

	public void setHouseService(IHouseService houseService) {
		this.houseService = houseService;
	}

	public IMeterDao getMeterDao() {
		return meterDao;
	}

	public void setMeterDao(IMeterDao meterDao) {
		this.meterDao = meterDao;
	}

	public IPeriodLogDao getPeriodLogDao() {
		return periodLogDao;
	}

	public void setPeriodLogDao(IPeriodLogDao periodLogDao) {
		this.periodLogDao = periodLogDao;
	}

}
